
//
//  ZZOrderVC.m
//  新项目
//
//  Created by 刘耀宗 on 2016/12/19.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

#import "ZZOrderVC.h"
#import "ZZOrderHeaderView.h"
#import "ZZOrderHeaderViewCell.h"
#import "WBPopMenuModel.h"
#import "WBPopMenuSingleton.h"
#import "ZZTimeModel.h"
#import "ZZOrderViewModel.h"
#import "ZZSettingVC.h"
#import "ZZOrderModel.h"
#import "ZZOrderContentModel.h"
#import "ZZUserModel.h"
@interface ZZOrderVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) ZZOrderHeaderView *header;
@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *mutaArr;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)NSString *currentMonthStr;
@property (nonatomic, strong) CcNoDataView *noDataView;

@end

@implementation ZZOrderVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
        [self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
         [self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];
}
-(NSMutableArray *)mutaArr
{
    if (!_mutaArr) {
        _mutaArr = [NSMutableArray array];
    }
    return _mutaArr;
}

-(CcNoDataView *)noDataView
{
    if (_noDataView == nil) {
        _noDataView = [[CcNoDataView alloc] init];
        _noDataView.backgroundColor=[UIColor whiteColor];
    }
    return _noDataView;
    
}

-(ZZOrderHeaderView *)header
{
    if (!_header) {
        _header = [ZZOrderHeaderView zzOrderHeaderViewFromXib];
       _header.frame = CGRectMake(0, 64, newwidth, 100);
        _header.backgroundColor = [UIColor lightGrayColor];
    }
    return _header;
}

- (void)viewDidLoad {
    self.title = @"订单管理";
    
    [super viewDidLoad];
    //self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
//    [self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];
   // self.view.backgroundColor=[UIColor whiteColor];
    [self getCurrentDate];

 //   [self initDataWithDateTime:self.currentMonthStr andPageIndex:@"1"];
  //  [self settitleView];
    [self createTableView];
    
    
    UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ic_top_date"]  style:UIBarButtonItemStylePlain target:self action:@selector(popDateMenu)];
    
    self.navigationItem.rightBarButtonItem = btn;
    
    
    [self refreshAction];
    [self.tableView.mj_header beginRefreshing];
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (!_header) {
        _header = [ZZOrderHeaderView zzOrderHeaderViewFromXib];
        _header.frame =CGRectMake(0, 0, newwidth, 100);
        _header.backgroundColor = [UIColor lightGrayColor];
    }
    return _header;
}



// 创建tableView
-(void)createTableView
{
    
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, newwidth, newheight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.separatorStyle=0;
        [self.view addSubview:_tableView];
    }
         [_tableView setTableHeaderView:[self header]];
    
}
-(void)settitleView
{
    UILabel *label=[[UILabel alloc] init];
    label.text=@"订单管理";
    label.textColor=[UIColor blackColor];
    [label sizeToFit];
    label.frame=CGRectMake(newwidth/2-label.frame.size.width/2, 20, label.frame.size.width, label.frame.size.height);
    self.navigationItem.titleView=label;


    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZZOrderHeaderViewCell *Cell = [ZZOrderHeaderViewCell cellWithTableVIew:tableView];
    Cell.model = _dataArr[indexPath.row];
    Cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return Cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

-(void)popDateMenu
{
    
    [self returnSixYearAndMonth];
    
    NSMutableArray *obj = [NSMutableArray array];
    
    for (NSInteger i = 0; i < _mutaArr.count; i++) {
        
        WBPopMenuModel * info = [WBPopMenuModel new];
        info.title = [self titles][i];
        [obj addObject:info];
    }
    
    NSLog(@"%@",obj);
    
    [[WBPopMenuSingleton shareManager]showPopMenuSelecteWithFrame:120
                                                             item: obj
                                                           action:^(NSInteger index) {
                                                               NSLog(@"index:%ld",(long)index);
                                                     
                                                               [self refreshSelectActionWithTitle:_mutaArr[index]];
                                                   [self initDataWithDateTime:_mutaArr[index] andPageIndex:1];
                                                               
                                                               
                                                               
                                                           }];

}

- (NSArray *) titles {
    DLog(@"%@",_mutaArr);
    
    return _mutaArr;
}


-(void)returnSixYearAndMonth
{
    // 获取当前秒
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSTimeInterval time1970 = [now timeIntervalSince1970];
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];
//    NSUInteger numberOfDaysInMonth = range.length;
    NSUInteger numberOfDaysInMonth = 30; 
      NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < 10 ; i++) {
        
        // 格式化时间
        NSDate *dateTime = [NSDate dateWithTimeIntervalSince1970:time1970-(86400*numberOfDaysInMonth*i)];
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
        [dateFormat setDateFormat:@"yyyy年MM月"];
        [dateFormat stringFromDate:dateTime];
        
        NSLog(@"%@",dateTime);
        
        //得到近6月时间
        NSString *string = [NSString stringWithFormat:@"%@",dateTime];
         NSLog(@"%@",string);
        ZZTimeModel *model = [[ZZTimeModel alloc]init];
        model.year = [string substringToIndex:4];
        model.month = [string substringWithRange:NSMakeRange(5, 2)];
        NSString *timeStr = [NSString stringWithFormat:@"%@年%@月",model.year,model.month];
        NSLog(@"%@",timeStr);
        [arr addObject:timeStr];
        _mutaArr = arr;
    }
    
    
}


-(void)initDataWithDateTime:(NSString *)dateTime andPageIndex:(NSInteger)pageIndex
{
    NSString *PageIndex = [NSString stringWithFormat:@"%ld",(long)pageIndex];
    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary   *loginDict = [loginDefaults objectForKey:@"loginInfo"];
    [[ZZOrderViewModel shareViewModel]getOrderWithCompanyID:loginDict[@"CompanyID"] andPageIndex:PageIndex andDateTime:dateTime andSuccess:^(NSDictionary *result) {
        
        if (result) {

                [ZZOrderModel mj_setupObjectClassInArray:^NSDictionary *{
                    return @{
                             @"rows" : @"ZZOrderContentModel",
                             };
                }];
                ZZOrderModel *fg = [ZZOrderModel mj_objectWithKeyValues:result[@"resultJson"]];
         
                self.total = [fg.total integerValue];
        
      
            if (!ceilf([fg.total floatValue] / 10.00)) {
                NSInteger x = 0;
                self.num = x;
            }else{
                NSInteger x = ceilf([fg.total floatValue] / 10.00);
                self.num = x;
            }
      
    
            if (self.index!=1) {
                if (self.index > self.num) {
                    [self.tableView reloadData];
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                     [_dataArr addObjectsFromArray:fg.rows];
                }
                   [self.tableView.mj_footer endRefreshing];
            }
               // _dataArr = fg.rows;
//                if (self.index!=1) {
//                    if (fg.rows.count==0) {
//                        [self.tableView reloadData];
//                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
//                    
//                    }else{
//                         [_dataArr addObjectsFromArray:fg.rows];
//                    }
//
//                    [self.tableView.mj_footer endRefreshing];
//                    
//                }
                else
                {
                    _dataArr = fg.rows;
                    
                    [self.tableView.mj_header endRefreshing];
                    
                }
                
                NSLog(@"%@",_dataArr);
                
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
      
            
                   [self.noDataView dataIsEmpty:_dataArr withView:self.tableView andNoDataView:self.noDataView andRect:CGRectMake(0, 104, newwidth, newheight-164) andBtnHiddne:YES];
        }
        
    } andFailer:^{
        [self.tableView.mj_header endRefreshing];
        [self.noDataView noNetwithView:self.tableView andNoDataView:self.noDataView andRect:CGRectMake(0, 0, newwidth, newheight) andBtnHiddne:NO];
        //点击重新加载的block
        WEAKSELF
        self.noDataView.myBlock=^{
            [weakSelf.tableView.mj_header beginRefreshing];
        };

    }];
}

-(void)toShowSetting
{
    ZZSettingVC *vc = [[ZZSettingVC alloc]init];

    [self.navigationController pushViewController:vc animated:YES];

}

-(void)getCurrentDate
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *nowdate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit;
    comps = [calendar components:unitFlags fromDate:nowdate];
    
    NSLog(@"%ld年",(long)[comps year]);
    NSLog(@"%ld月",(long)[comps month]);
    NSLog(@"%ld日",(long)[comps day]);
    NSLog(@"%ld时",(long)[comps hour]);
    NSLog(@"%ld分",(long)[comps minute]);
    NSLog(@"%ld秒",(long)[comps second]);
    
    self.currentMonthStr = [NSString stringWithFormat:@"%ld年%ld月",(long)[comps year],(long)[comps month]];
    
}

- (void)refreshAction {
    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataArr removeAllObjects];
        self.index = 1;

        self.header.MonthIncomeLabel.text = [NSString stringWithFormat:@"¥%@",[ZZUserModel shareViewModel].monthAllMoney];
        self.header.todayIncomeLabel.text = [NSString stringWithFormat:@"¥%@",[ZZUserModel shareViewModel].todayMoney];
        [_tableView reloadData];
        [self initDataWithDateTime:self.currentMonthStr andPageIndex:self.index];
    }];
    
    // 上拉刷新
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.index = self.index + 1;
        [self initDataWithDateTime:self.currentMonthStr andPageIndex:self.index];
    }];
}


-(void)refreshSelectActionWithTitle:(NSString *)title  {
    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataArr removeAllObjects];
        self.index = 1;
        [_tableView reloadData];
        [self initDataWithDateTime:title andPageIndex:self.index];
    }];
    
    // 上拉刷新
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.index = self.index + 1;
        [self initDataWithDateTime:self.currentMonthStr andPageIndex:self.index];
    }];
}


@end
